
#include "Resources.h"
#include <Ogre.h>
#include <fstream>
#include <set>

namespace
{

	class OrderResources
	{
	public:
		bool operator() (Ogre::ResourcePtr const & r1, Ogre::ResourcePtr const & r2) const
		{
			return r1.useCount() > r2.useCount();
		}
	};
}

void DbgShowUnusedResources()
{
	std::ofstream fout("resources.log");

	Ogre::Root * root = Ogre::Root::getSingletonPtr();
	Ogre::ResourceGroupManager * rgrpMngr = Ogre::ResourceGroupManager::getSingletonPtr();
	Ogre::ResourceGroupManager::ResourceManagerIterator itmngr = 
		rgrpMngr->getResourceManagerIterator();

	typedef std::multiset<Ogre::ResourcePtr, OrderResources> ResSet;
	ResSet resSet;
	
	while(itmngr.hasMoreElements())
	{
		Ogre::ResourceManager * mngr = itmngr.getNext();
		Ogre::ResourceManager::ResourceMapIterator itres = mngr->getResourceIterator();

		while(itres.hasMoreElements())
		{
			Ogre::ResourcePtr res = itres.getNext();

			resSet.insert(res);
		}
	}

	for(ResSet::iterator it = resSet.begin(); it != resSet.end(); ++it)
	{
		fout <<  (*it)->getName() << " " << (*it)->getOrigin() << " " << (*it).useCount()
			<< '\n';
	}
}